package com.kye.express.business.jpush;

import android.content.Context;

import com.kye.express.R;
import com.kye.express.business.order_detail.OrderDetailActivity;
import com.kye.express.business.waybill.waybill_detail.WayBillDetailActivity;
import com.kye.express.business.web.KyeWebActivity;
import com.kye.model.UserSession;
import com.kye.model.bean.jpush.PushCommentModel;
import com.kye.model.bean.jpush.PushFeedbackModel;
import com.kye.model.bean.jpush.PushHttpLogModel;
import com.kye.model.bean.jpush.PushNewsModel;
import com.kye.model.bean.jpush.PushPlaceOrderModel;
import com.kye.model.bean.jpush.PushSplashModel;
import com.kye.model.bean.jpush.PushType;
import com.kye.model.bean.waybill_search.ResultWaybillQuery;

import java.util.List;

/**
 * push消息处理类(业务分发)
 */
public class JPushResponseParseHelper {

    public Context mContext;
    public PushSplashModel mPushSplashBean;
    private static JPushResponseParseHelper mInstance;

    private JPushResponseParseHelper() {

    }

    public static JPushResponseParseHelper getInstance() {
        if (mInstance == null) {
            synchronized (JPushResponseParseHelper.class) {
                if (mInstance == null) {
                    mInstance = new JPushResponseParseHelper();
                }
            }
        }
        return mInstance;
    }

    public JPushResponseParseHelper init(Context context, PushSplashModel pushSplashBean) {
        this.mContext = context;
        this.mPushSplashBean = pushSplashBean;
        return mInstance;
    }


    /**
     * 开始解析
     */
    public void startParseJPushResponse() {
        if (mPushSplashBean != null && mContext != null) {
            PushType pushType = mPushSplashBean.getPushType();
            if (PushType.news == pushType) {//新闻 消息
                parsePushNews(mPushSplashBean);
            } else if (PushType.order == pushType) {//订单消息
                parseOrder(mPushSplashBean);
            } else if (PushType.express == pushType) {//运单消息
                parseExpress(mPushSplashBean);
            } else if (PushType.otherLogin == pushType) {//下线通知
                parseOtherLogin(mPushSplashBean);
            } else if (PushType.noticeDialog == pushType) {//弹窗通知  界面弹出弹框
                parseNotice(mPushSplashBean);
            } else if (PushType.noticeDetatil == pushType) {//弹窗通知   通知栏直接跳转通知详情界面
                parseNoticeDetail(mPushSplashBean);
            } else if (PushType.commentOne == pushType || PushType.commentTwo == pushType) {//通知二级评论  //通知一级评论
                parseNoticeComment(pushType, mPushSplashBean);
            } else if (PushType.feedBack == pushType) {//反馈
                parseFeedBack(mPushSplashBean);
            } else if (PushType.sendLog == pushType) {//上传接口日志
                parseSendHttpLog(mPushSplashBean);
            }
            JpushNotificationUtils.savePushSplashBean(null);
        }
    }

    /**
     * 推送消息 新闻类型
     *
     * @param pushSplashBean
     */
    public void parsePushNews(PushSplashModel pushSplashBean) {
        PushNewsModel pushNewsModel = (PushNewsModel) pushSplashBean.getPushBean(PushNewsModel.class.getName());
        if (pushNewsModel != null) {
            KyeWebActivity.startActivityForNewTask(mContext, pushNewsModel.getAccessUrl(), mContext.getString(R.string.found_kye_news), true);
        }
    }


    /**
     * 推送消息 下单消息
     *
     * @param pushSplashBean
     */
    public void parseOrder(PushSplashModel pushSplashBean) {
        PushPlaceOrderModel pushPlaceOrderModel = (PushPlaceOrderModel) pushSplashBean.getPushBean(PushPlaceOrderModel.class.getName());
        String phone = "";
        if (UserSession.getInstance().getUser() != null) {
            phone = UserSession.getInstance().getUser().getPhone();
        }
        OrderDetailActivity.newStartIntentForNewTask(mContext, phone, pushPlaceOrderModel.getMsgId(), "5");
    }

    /**
     * 推送消息 运单消息
     *
     * @param pushSplashBean
     */
    public void parseExpress(PushSplashModel pushSplashBean) {
        ResultWaybillQuery resultWaybillQuery = (ResultWaybillQuery) pushSplashBean.getPushBean(ResultWaybillQuery.class.getName());
        if (resultWaybillQuery != null) {
            List<ResultWaybillQuery.ResultBean> resultBeans = resultWaybillQuery.getResult();
            if (resultBeans != null && resultBeans.size() > 0) {
                if (resultBeans.get(0) != null) {
                    String waybill = resultBeans.get(0).getWaybill();
                    WayBillDetailActivity.startActivityForNewTask(mContext, waybill);
                }
            }
        }
    }

    /**
     * 下线 通知
     */
    public void parseOtherLogin(PushSplashModel pushSplashBean) {
        PushPlaceOrderModel pushPlaceOrderModel = (PushPlaceOrderModel) pushSplashBean.getPushBean(PushPlaceOrderModel.class.getName());
        if (pushPlaceOrderModel != null) {
            JpushDialogActivity.showOtherLoginDialog(mContext, pushPlaceOrderModel);
        }
    }

    /**
     * 弹窗通知   对话框通知
     *
     * @param pushSplashBean
     */
    public void parseNotice(PushSplashModel pushSplashBean) {
        PushNewsModel pushNewsModel = (PushNewsModel) pushSplashBean.getPushBean(PushNewsModel.class.getName());
        if (pushNewsModel != null) {
            JpushDialogActivity.showNoticeDialog(mContext, pushNewsModel);
        }
    }

    /**
     * 弹窗   通知详情
     *
     * @param pushSplashBean
     */
    public void parseNoticeDetail(PushSplashModel pushSplashBean) {
        PushNewsModel mPushNewsModel = (PushNewsModel) pushSplashBean.getPushBean(PushNewsModel.class.getName());
        //TODO 跳转到通知详情, 注意：JpushDialogFragment中也有一个跳转到通知详情的需要完善
    }

    /**
     * //通知二级评论  //通知一级评论
     *
     * @param pushSplashBean
     */
    public void parseNoticeComment(PushType pushType, PushSplashModel pushSplashBean) {
        PushCommentModel pushCommentModel = (PushCommentModel) pushSplashBean.getPushBean(PushCommentModel.class.getName());
        // TODO 跳转到评论页面？
    }

    /**
     * 反馈 通知
     */
    public void parseFeedBack(PushSplashModel pushSplashBean) {
        PushFeedbackModel pushFeedbackModel = (PushFeedbackModel) pushSplashBean.getPushBean(PushFeedbackModel.class.getName());
        if (pushFeedbackModel != null) {
            JpushDialogActivity.showFeedbackDialog(mContext, pushFeedbackModel);
        }
    }

    /**
     * 上传接口日志
     *
     * @param pushSplashBean
     */
    public void parseSendHttpLog(PushSplashModel pushSplashBean) {
        PushHttpLogModel pushHttpLogModel = (PushHttpLogModel) pushSplashBean.getPushBean(PushHttpLogModel.class.getName());
        //TODO 上传httplog到服务器，目前似乎还没有保存log到sdcard
    }
}